package com.ziyun.pop.modules.storeGoodsCategory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.storeGoodsCategory.dao.StoreGoodsCategoryDao;
import com.ziyun.pop.modules.storeGoodsCategory.entity.StoreGoodsCategoryEntity;
import com.ziyun.pop.modules.storeGoodsCategory.service.StoreGoodsCategoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("storeGoodsCategoryService")
public class StoreGoodsCategoryServiceImpl extends ServiceImpl<StoreGoodsCategoryDao, StoreGoodsCategoryEntity> implements StoreGoodsCategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<StoreGoodsCategoryEntity> page = (Page<StoreGoodsCategoryEntity>) this.page(
                new Query<StoreGoodsCategoryEntity>(params).getPage(),
                new QueryWrapper<StoreGoodsCategoryEntity>()
        );

        return new PageUtils(page);
    }




    @Override
    public List<StoreGoodsCategoryEntity> getStoreGoodsCategoryList() {
        //先查出所有分类
        List<StoreGoodsCategoryEntity> firstList=this.list(new QueryWrapper<StoreGoodsCategoryEntity>().eq("del",0));
        //然后调用递归方法查询出所有分类
        List<StoreGoodsCategoryEntity> categoryData= categoryTreeHandle(firstList,0);
        return categoryData;
    }

    /**
     * 获取所有store_goods_category表中的商务印刷分类
     * @return
     */
    @Override
    public List<StoreGoodsCategoryEntity> getPrintCategoryList() {
        return this.baseMapper.getPrintCategoryList();
    }

    /**
     * 获取所有store_goods_category表中的快印分类
     * @return
     */
    @Override
    public List<StoreGoodsCategoryEntity> getFastCateoryList() {
        return this.baseMapper.getFastCateoryList();
    }

    @Override
    /**
     * 根据登录人的卖家id,获得是工厂还是小b
     */
    public String getSellerType(Long sellerId) {
        return this.baseMapper.getSellerType(sellerId);
    }

  /*  //递归查询子分类
    public List<StoreGoodsCategoryEntity> getNode(List<StoreGoodsCategoryEntity> firstList){
        for(int i=0;i<firstList.size();i++){
            StoreGoodsCategoryEntity goodsCategoryEntity=firstList.get(i);
            //查询标准商品封装参数
            Map<String,Object> params=new HashMap<>();
            params.put("parent_category_id",goodsCategoryEntity.getCategoryId());
            params.put("del",0);
            params.put("goods_type","standard");
            List<StoreGoodsCategoryEntity> child=this.baseMapper.getStandardList(params);
            getNode(child);
            goodsCategoryEntity.setChildren(child);
        }
        return  firstList;
    }*/


    /**
     * 分类树处理
     * @param storeGoodsCategoryList
     * @param pid
     * @return
     */
    private List<StoreGoodsCategoryEntity> categoryTreeHandle(List<StoreGoodsCategoryEntity> storeGoodsCategoryList, int pid) {
        List<StoreGoodsCategoryEntity> categoryTreeList = new ArrayList<>();

        //获取顶级元素集合
        Integer parentId;
        for (StoreGoodsCategoryEntity storeGoodsCategory : storeGoodsCategoryList) {
            parentId = storeGoodsCategory.getParentCategoryId();
            if (parentId == null || pid == parentId) {
                categoryTreeList.add(storeGoodsCategory);
            }
        }

        //获取每个顶层元素的子数据集合
        for (StoreGoodsCategoryEntity categoryTree : categoryTreeList) {
            List<StoreGoodsCategoryEntity> temp = categoryTreeHandle(storeGoodsCategoryList, categoryTree.getCategoryId());
            if (temp.size() > 0) {
                categoryTree.setChildren(temp);
            }
        }
        return categoryTreeList;
    }
}
